/*
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

package com.bstek.dorado.view.output;

import java.io.Writer;
import java.util.Collection;

import com.bstek.dorado.core.el.Expression;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;

/**
 * @author Benny Bao (mailto:benny.bao@bstek.com)
 * @since 2011-6-14
 */
public class DoradoMapOutputter extends DataOutputter {

	@Override
	public void output(Object object, OutputContext context) throws Exception {
		if (object != null && !EntityUtils.isSimpleValue(object)
				&& !(object instanceof Collection<?>)
				&& !(object instanceof Page<?>)
				&& !(object instanceof Expression)) {
			JsonBuilder json = context.getJsonBuilder();
			json.beginValue();
			Writer writer = context.getWriter();
			writer.write("$map(");
			super.output(object, context);
			writer.write(")");
			json.endValue();
		} else {
			super.output(object, context);
		}
	}
}
